package com.homwee.factory.util.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.homwee.factory.R;
import com.homwee.tvap.common.ap.ComSettingManager;
import com.homwee.tvap.common.srvfunc.FactorySetting;

public class FactoryKeyResultDialog extends AlertDialog implements OnClickListener {
	private Context mContext;
	private static Boolean[] mKeyDirExists = null;
	private static Boolean[] mKeyFileExists = null;
	private static Boolean[] mKeyFileUpdates = null;
	private static Boolean[] mKeyFileTvExists = null;
	private Button okButton;
	private ListView mListView;
	public ComSettingManager mcomSettingManager;
	public FactorySetting factorySetting;
	private KeyDetailAdapter mKeyDetailAdapter;
	private static boolean  isShow=false;
	private static FactoryKeyResultDialog mInstance;
	
	public FactoryKeyResultDialog(Context context,
			Boolean[] mKeyDirExistsBooleans, Boolean[] mKeyFileExistsBooleans,
			Boolean[] mKeyFileUpdatesBooleans,Boolean[] mKeyFileTvExistsBooleans) {
		super(context, R.style.Theme_FactorySettingDialog);
		mContext = context;
		mKeyDirExists = mKeyDirExistsBooleans;
		mKeyFileExists = mKeyFileExistsBooleans;
		mKeyFileUpdates = mKeyFileUpdatesBooleans;
		mKeyFileTvExists= mKeyFileTvExistsBooleans;
		if (context instanceof Activity) {
			
		}
		mContext = context;
	}

	public static FactoryKeyResultDialog getInstance(Context context,
			Boolean[] mKeyDirExistsBooleans, Boolean[] mKeyFileExistsBooleans,
			Boolean[] mKeyFileUpdatesBooleans,Boolean[] mKeyFileTvExistsBooleans) {
		if(mInstance==null){
			mInstance=new  FactoryKeyResultDialog(context, mKeyDirExistsBooleans,
					mKeyFileExistsBooleans, mKeyFileUpdatesBooleans,mKeyFileTvExistsBooleans);
		}
		else{
			refresh(mKeyDirExistsBooleans, mKeyFileExistsBooleans, mKeyFileUpdatesBooleans,mKeyFileTvExistsBooleans);
		}
		return mInstance;
	}
	
	public FactoryKeyResultDialog(Context context, AttributeSet attrs) {
		super(context, R.style.Theme_FactorySettingDialog);
		mContext = context;
		if (context instanceof Activity) {
		}
		mContext = context;
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mcomSettingManager = ComSettingManager.getInstance(mContext);
		factorySetting = mcomSettingManager.getFactory();
		setContentView(R.layout.factory_key_result_dialog);
		initView();

	}

	public void initView() {
		mListView = (ListView) findViewById(R.id.key_detail_listView);
		mKeyDetailAdapter=new KeyDetailAdapter(mContext,
				mKeyDirExists, mKeyFileExists,
				mKeyFileUpdates,mKeyFileTvExists);
		mListView.setAdapter(mKeyDetailAdapter);
		mListView.setItemsCanFocus(false);
		mListView.setEnabled(false);
		okButton = (Button) findViewById(R.id.okButton);
		okButton.setFocusable(true);
		okButton.requestFocus();
		// cancelButton = (Button)findViewById(R.id.cancelButton);
		// cancelButton.setOnClickListener(this);
		okButton.setOnClickListener(this);
	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.okButton:{
			mHandler.removeCallbacks(mRun);
			dismiss();
		}
			break;
		case R.id.cancelButton:
			cancel();
			break;
		}
	}
	 private Handler mHandler = new Handler();
	private Runnable mRun = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            dismiss();
        }
    };
	@Override
	public void show() {
		// TODO Auto-generated method stub
		if(!isShow){
			isShow=true;
			mHandler.postDelayed(mRun, 3000);
			super.show();
		}
		else {
			Log.d("zcj", "update");
			mKeyDetailAdapter.updateDate(mKeyDirExists, mKeyFileExists, mKeyFileUpdates,mKeyFileTvExists);
			mKeyDetailAdapter.notifyDataSetChanged();
		}
	}
	@Override
	public void dismiss() {
		// TODO Auto-generated method stub
		isShow=false;
		mKeyDirExists=null;
		mKeyFileExists=null;
		mKeyFileUpdates=null;
		mKeyFileTvExists=null;
		super.dismiss();
	}
	
	public static void refresh(Boolean[] mKeyDirExistsBooleans, Boolean[] mKeyFileExistsBooleans,
			Boolean[] mKeyFileUpdatesBooleans,Boolean[] mKeyFileTvExistsBooleans) {
		mKeyDirExists = mKeyDirExistsBooleans;
		mKeyFileExists = mKeyFileExistsBooleans;
		mKeyFileUpdates = mKeyFileUpdatesBooleans;
		mKeyFileTvExists=mKeyFileTvExistsBooleans;
		
		Log.d("zcj", mKeyFileUpdates[1]?"true":"false");
	}

}

class KeyDetailAdapter extends BaseAdapter {

	Boolean[] mKeyDirExistsBooleans = null;
	Boolean[] mKeyFileExistsBooleans = null;
	Boolean[] mKeyFileUpdatesBooleans = null;
	Boolean[] mKeyFileTvExistsBooleans = null;
	private static String[] mKeyPathStrings = null;
	Context context = null;

	public KeyDetailAdapter() {
		// TODO Auto-generated constructor stub
	}

	public KeyDetailAdapter(Context context, Boolean[] mKeyDirExistsBooleans,
			Boolean[] mKeyFileExistsBooleans, Boolean[] mKeyFileUpdatesBooleans,Boolean[] mKeyFileTvExistsBooleans) {
		// TODO Auto-generated constructor stub
		this.mKeyDirExistsBooleans = mKeyDirExistsBooleans;
		this.mKeyFileExistsBooleans = mKeyFileExistsBooleans;
		this.mKeyFileUpdatesBooleans = mKeyFileUpdatesBooleans;
		this.mKeyFileTvExistsBooleans=mKeyFileTvExistsBooleans;
		this.context = context;
		mKeyPathStrings = this.context.getResources().getStringArray(
				R.array.supprtKeyNameListUIName);
	}

	public void updateDate(Boolean[] mKeyDirExistsBooleans,
			Boolean[] mKeyFileExistsBooleans, Boolean[] mKeyFileUpdatesBooleans,Boolean[] mKeyFileTvExistsBooleans) {
		this.mKeyDirExistsBooleans = mKeyDirExistsBooleans;
		this.mKeyFileExistsBooleans = mKeyFileExistsBooleans;
		this.mKeyFileUpdatesBooleans = mKeyFileUpdatesBooleans;
		this.mKeyFileTvExistsBooleans = mKeyFileTvExistsBooleans;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mKeyDirExistsBooleans.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mKeyDirExistsBooleans[position];
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder mViewHolder;

		if (convertView == null) {
			mViewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.factory_key_result_list_item, null);
			mViewHolder.title = (TextView) convertView
					.findViewById(R.id.factory_textview_name);
			mViewHolder.content1 = (TextView) convertView
					.findViewById(R.id.factory_textview_value1);
			mViewHolder.content2 = (TextView) convertView
					.findViewById(R.id.factory_textview_value2);
			mViewHolder.content3 = (TextView) convertView
					.findViewById(R.id.factory_textview_value3);
			mViewHolder.content4 = (TextView) convertView
					.findViewById(R.id.factory_textview_value4);
			convertView.setTag(mViewHolder);
		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}

		mViewHolder.title.setText(mKeyPathStrings[position]);

		if (!mKeyDirExistsBooleans[position]) {
			mViewHolder.content1.setText(context.getString(R.string.factory_pagetwo_key_folder_no_exits));
			mViewHolder.content1.setVisibility(View.VISIBLE);
			mViewHolder.content2.setVisibility(View.GONE);
			mViewHolder.content3.setVisibility(View.GONE);
			mViewHolder.content4.setVisibility(View.GONE);
		} else if (!mKeyFileExistsBooleans[position]) {
			mViewHolder.content2.setText(context.getString(R.string.factory_pagetwo_key_file_no_exits));
			mViewHolder.content1.setVisibility(View.GONE);
			mViewHolder.content2.setVisibility(View.VISIBLE);
			mViewHolder.content3.setVisibility(View.GONE);
			mViewHolder.content4.setVisibility(View.GONE);
		} else if (mKeyFileTvExistsBooleans[position]) {
			mViewHolder.content4.setText(context.getString(R.string.factory_pagetwo_key_exits));
			mViewHolder.content1.setVisibility(View.GONE);
			mViewHolder.content2.setVisibility(View.GONE);
			mViewHolder.content3.setVisibility(View.GONE);
			mViewHolder.content4.setVisibility(View.VISIBLE);
		} else {
			if (mKeyFileUpdatesBooleans[position]) {
				mViewHolder.content3.setText(context.getString(R.string.factory_pagetwo_key_update_success));
				mViewHolder.content3.setTextColor(context.getColor(R.color.solid_green));
			} else {
				mViewHolder.content3.setText(context.getString(R.string.factory_pagetwo_key_update_error));
				mViewHolder.content3.setTextColor(context.getColor(R.color.solid_red));
			}
			mViewHolder.content1.setVisibility(View.GONE);
			mViewHolder.content2.setVisibility(View.GONE);
			mViewHolder.content3.setVisibility(View.VISIBLE);
			mViewHolder.content4.setVisibility(View.GONE);
			
			Log.d("zcj", "touch");
		}
		convertView.setEnabled(false);
		return convertView;
	}

}

class ViewHolder {
	TextView title;
	TextView content1;
	TextView content2;
	TextView content3;
	TextView content4;
}
